import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import javax.swing.table.AbstractTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

//this panel holds the table and the buttons to manage the table
public class AddClientPanel extends JPanel implements ActionListener{
    private boolean DEBUG = false;
    private JTextField filterText;
  	 ScreenedClientList list;  
	 private JTable table;
    private TableRowSorter<ScreenedClientList> sorter;
    private JButton addClientButton;
	 private JButton deleteClientButton;
	 private JScrollPane scrollPane;
        
    public AddClientPanel(){
	 	list = new ScreenedClientList();
                
    	this.setBackground(Color.WHITE);
      
		
      sorter = new TableRowSorter<ScreenedClientList>(list);
      table = new JTable(list);
      table.setRowSorter(sorter);
      table.setPreferredScrollableViewportSize(new Dimension(400, 70));
      table.setFillsViewportHeight(true);
      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      
		//When selection changes, outputs status bar for current table location
      table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                    
						  public void valueChanged(ListSelectionEvent event) {
                    		int viewRow = table.getSelectedRow();
                    	
								if (viewRow < 0) {
                    			//Selection got filtered away.
                    			System.out.println("");
                    		} 
						  		else {
                    			int modelRow = table.convertRowIndexToModel(viewRow);
                     		System.out.println( String.format("Selected Row in view: %d. " +"Selected Row in model: %d.", viewRow, modelRow));
                    		}
                    }
      		}
        );
              
      //Create a separate form for 
      //                - filterText search bar - filters by first name currently, needs to filter last also
      //                - statusText in console
      //                - openPatientFile button
        JPanel form = new JPanel(new GridLayout(3,2));
        form.setBackground(Color.WHITE);
        
        // search/filter bar
        JLabel l1 = new JLabel("Search By First Name", SwingConstants.TRAILING);
        form.add(l1);
        filterText = new JTextField();
        filterText.setColumns(10);
            //Whenever filterText changes, invoke newFilter.
                filterText.getDocument().addDocumentListener(
                        new DocumentListener() {
                            public void changedUpdate(DocumentEvent e) {
                                newFilter();
                            }
                            public void insertUpdate(DocumentEvent e) {
                                newFilter();
                            }
                            public void removeUpdate(DocumentEvent e) {
                                newFilter();
                            }
                        });
        
        l1.setLabelFor(filterText);
        form.add(filterText);		
			
        addClientButton = new JButton("Add Client");
		  addClientButton.addActionListener(this);
		  deleteClientButton = new JButton("Delete Client");
		  deleteClientButton.addActionListener(this); 
        form.add(addClientButton);
		  form.add(deleteClientButton);
        
        scrollPane = new JScrollPane(table);
        this.add(form);          
        this.add(scrollPane);            
       }
        
        
    //no sure what this is used for I copied it from kevins code???
    private void newFilter() {
        RowFilter<ScreenedClientList, Object> rf = null;
        try {
            rf = RowFilter.regexFilter(filterText.getText(), 0);
        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }
        sorter.setRowFilter(rf);
    }
    
	 public void addRow(ScreenedClient sc){	
			list.addClient(sc);
			//list.printList();
     }
	  
	  //this statement will refresh the table after you made changes
     public void refreshTable(){
	  	list.fireTableDataChanged();
		}
		
	 //note that table.getSelectedRow() will give you the index of the highlighted row that you clicked on in the table
   public void actionPerformed(ActionEvent evt){
	 	if( evt.getSource() == addClientButton){
			System.out.println(list.getIndexOf(table.getSelectedRow())); //this will have to link up with the PDF portion of the program
		}
		else if(evt.getSource() == deleteClientButton){
			list.removeAt(table.getSelectedRow());
		}
	}
}
        

